<template>
  <!--  最外层大盒子，用于装左侧主题展示区和右侧的登录界面区-->
  <div class="main">
    <!--左侧主题展示区-->
    <!--和登录界面一样，因此公用一个css样式-->
    <div class="background">
      <img src="../assets/images/login-background.jpg" alt="登录界面左边图片">
      <div class="background-title">
        <div class="background-title-box">
          <span class="background-title-theme">智能守护 健康同行</span>
          <span class="background-title-introduce">专业级健康监护  24小时贴心守护</span>
        </div>
        <div class="background-introduce-box">
          <div class="background-introduce1">
            <i class="el-icon-monitor"></i>
            <span class="theme">实时监控</span>
            <span class="introduce">持续追踪心率、血氧、睡眠等健康指标</span>
          </div>
          <div class="background-introduce2">
            <i class="el-icon-data-analysis"></i>
            <span class="theme">数据分析</span>
            <span class="introduce">智能算法分析，提供个性化健康建议</span>
          </div>
        </div>
      </div>
    </div>
    <!--    右侧注册界面区-->
  <div class="login">
    <div class="login-form-title">
      <img src="../assets/logo/logo.png" alt="logo">
      <h3 class="title">健康穿戴管理系统</h3>
    </div>
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form">
      <el-form-item prop="username" class="account">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="邮箱或手机号码">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" class="secret">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword" class="secret">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
<!--      <el-form-item prop="phone">-->

<!--      </el-form-item>-->
<!--这里需要改成手机验证码或邮箱验证码-->
      <div class="code">
        <el-form-item prop="code" class="code-input" >
          <el-input v-model="registerForm.code" auto-complete="off" placeholder="验证码" @keyup.enter.native="handleRegister">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
            <img :src="codeUrl" @click="getCode" class="login-code-img"  alt="验证码"/>
      </div>

      <el-form-item >
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          @click.native.prevent="handleRegister"
          class="login-button"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div class="register-login">
          <span>已有账户？</span>
          <router-link class="link-type" :to="'/login'">立即登陆</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 kangyue.vip All Rights Reserved.</span>
    </div>
  </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: false,
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style >
.main{
  display:flex;
  width: 100%;
  height: 100%;
  border-radius: 10px;
}
.background{
  position: relative;
  width: 60%;
  height: 100%;
  display: flex;
}
.background img{
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;

}
.background .background-title{
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(to left, rgba(66, 144, 222, 0.66), rgb(66, 74, 222));
  z-index: 1;
  display: flex;
  //justify-content: center;
  flex-direction: column;

}
.background-title-box {
  display: flex;
  flex-direction: column;
  margin-top: 20%;
}
.background-title-theme{
  font-size: 40px;
  font-weight: bold;
  color: white;
  margin-left: 60px;
  margin-bottom: 15px;
}
.background-title-introduce{
  font-size: 12px;
  margin-left: 60px;
  color: white;
  margin-bottom: 15px;
}
.background-introduce-box{
  display: flex;
  margin-top: 5%;

}
.background-introduce1{
  display: flex;
  flex-direction: column;
  margin-left: 60px;
  background-color: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 10px;
}
.background-introduce2{
  display: flex;
  flex-direction: column;
  margin-left: 20px;
  background-color: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 10px;
}
.el-icon-monitor{
  color: white;
  margin: 5px;
}
.el-icon-data-analysis{
  color: white;
  margin: 5px;
}
.theme{
  font-size: 12px;
  font-weight: bold;
  color: white;
  margin: 5px;
}
.introduce{
  font-size: 10px;
  color: white;
  margin: 5px;
}
.login{
  width:40%;
  height: 100%;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
}
.login-form-title{
  display: flex;
  height: 20%;
  width: 100%;
  align-items: center;
}
.login-form-title img{
  width: 40px;
  height: 40px;
  margin-left: 10%;
  margin-right: 10px;
}
.login-form-title h3{

  font-size: 30px;
  font-weight: bold;
}
.login-form{
  height: 70%;
  width: 100%;
  display: flex;
  flex-direction: column;

}
.account{
  margin-top: 15%;
  margin-left: 20%;
  width: 60%;
}
.secret{
  margin-left: 20%;
  width: 60%;
}
.code{
  display: flex;
}
.code .code-input{
  margin-left: 20%;
  width: 40%;
}
.code img{
  margin-left: 5px;
  width: 19%;
  height: 62%;
  border-radius: 3px;
}
.register-code{
  margin-left: 5px;
  width: 30%;
  height: 62%;
}
.register-code-button{
  height: 100%;
}
.login-button{
  margin-top: 3%;
  margin-left: 20%;
  width: 60%;
}
.register-login{
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
}
.el-login-footer{
  display: flex;
  height: 10%;
  justify-content: center;
  align-items: center;
}
</style>
